using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIDesigner
{
  class CtrlDataMaker
  {
    public static string OutCode(Control.ControlCollection cc)
    {
      string str = "";
      foreach (System.Windows.Forms.Control c in cc)
      {
        //CreateWindowA("BUTTON", "戻る", WS_CHILD | WS_VISIBLE, 447, 401, 113, 28, hWnd, (HMENU)NULL, hInst, NULL);
          str += "    ";
        if (!(c is Label))
        {
          str += c.Name + " = ";
        }
        str += "CreateWindowA(" +
          "\"" + ControlString(c) + "\", " +
          "\"" + c.Text + "\", " +
          "WS_CHILD | WS_VISIBLE, " +
          c.Left + ", " +
          c.Top + ", " +
          (c.Right - c.Left + 1) + ", " +
          (c.Bottom - c.Top + 1) + ", " +
          "hWnd, ";
        if (c is Label)
        {
          str += "NULL";
        }
        else
        {
          str += "(HMENU)" + IdString(c);
        }
        str += ", hInst, NULL);\r\n";
      }

      return str;
    }
    public static string OutDetails(Control.ControlCollection cc)
    {
      string str = "";
      int i = 0;
      foreach (System.Windows.Forms.Control c in cc)
      {
        str += "種類=" + ControlString(c) + ",";
        str += "Name=" + c.Name + ",Text=" + c.Text;
        str += ",Left=" + c.Left + ",Top=" + c.Top + ",Right=" + c.Right + ",Bottom=" + c.Bottom;
        str += "\r\n";
        i++;
      }
      str += "\r\n合計＝" + i;

      return str;
    }

    public static string OutNormal(Control.ControlCollection cc)
    {
      string str = "";
      int i = 0;
      foreach (System.Windows.Forms.Control c in cc)
      {
        str += ControlString(c) + ",";
        str += c.Name + " - " + c.Text;
        str += "(" + c.Left + "," + c.Top + ")-(" + c.Right + "," + c.Bottom + ")";
        if (c is TextBox)
        {
          TextBox t = (TextBox)c;
          str += "," + t.TabIndex;
        }
        else if (c is Label)
        {
          Label l = (Label)c;
          if (l.Font.Bold) str += "Bold";
        }
        str += "\r\n";
        i++;
      }
      str += "\r\n合計＝" + i;

      return str;
    }

    public static string OutCSV(Control.ControlCollection cc)
    {
      string str = "";
      int i = 0;
      foreach (System.Windows.Forms.Control c in cc)
      {
        str += ControlString(c) + ",";
        str += c.Name + "," + c.Text;
        str += "," + c.Left + "," + c.Top + "," + c.Right + "," + c.Bottom + ",";
        if (c is TextBox)
        {
          TextBox t = (TextBox)c;
          str += t.TabIndex;
        }
        else if (c is Label)
        {
          Label l = (Label)c;
          if (l.Font.Bold) str += "Bold";
        }
        str += "\r\n";
        i++;
      }

      return str;
    }

    public static string OutWinProc(Control.ControlCollection cc)
    {
      int id;
      string str = "";
      str += @"#pragma comment(linker, ""/manifestdependency:\""type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"""")

#include <Windows.h>

";
      id = 100;
      foreach (System.Windows.Forms.Control c in cc)
      {
        if (!(c is Label))
        {
          str += "const int " + IdString(c) + " = " + id + "; \r\n";
          id++;
        }
      }

      str += @"
HINSTANCE hInst;

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  static HWND " + DefinitionString<TextBox>(cc);
      str += "  static HWND " + DefinitionString<Button>(cc);
      str += @"
  switch (message) {
  case WM_CREATE:
" + OutCode(cc);
      str += @"
    return 0;
  case WM_COMMAND:
";
      foreach (System.Windows.Forms.Control c in cc)
      {
        if (c is Button)
        {
          str += "    if (LOWORD(wParam) == " + IdString(c) + ") {\r\n    }\r\n";
        }
      }
      str += @"    return 0;

  case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
  }
  return DefWindowProc(hWnd , message , wParam , lParam);
}
";
      return str;
    }

    private static string ControlString(Control c)
    {
      if (c is TextBox) return "EDIT";
      if (c is Label) return "STATIC";
      if (c is Button) return "BUTTON";
      return "不明";
    }
    private static string IdString(Control c)
    {
      return "ID_" + c.Name.ToUpper();
    }
    private static string DefinitionString<T>(Control.ControlCollection cc)
    {
      string str = "";
      bool is_first = true;
      foreach (System.Windows.Forms.Control c in cc)
      {
        if (c is T)
        {
          if (is_first)
          {
            is_first = false;
          }
          else
          {
            str += ", ";
          }
          str += c.Name;
        }
      }
      return str + ";\r\n";
    }
  }
}
